home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
526-550
/
disk_534
/
term
/
libs.lzh
/
XprXModem
/
dlink.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-26
|
12KB
|
491 lines
/* dlink - data link transfer routines for bmodem */
/*
* by David Betz, BYTE Magazine/BIX
*
* statements involving bm_infoinit(), bm_info() added by Willy Langeveld for
* VLT
*
* ANSIsized and made re-entrant by Marc Boucher for XPR implementation.
*
*/
#include <string.h>
#include <stdio.h>
#include "xprxmodem.h"
#include "proto-dlink.h"
#include "proto-xprxmodem.h"
#include "proto-callback.h"
/* useful definitions */
#define TRUE 1
#define FALSE 0
/* protocol characters */
#define SOH 0x01 /* start of a 128 byte block */
#define STX 0x02 /* start of a 1024 byte block */
#define EOT 0x04 /* end of a complete file */
#define ACK 0x06 /* positive acknowledgement */
#define NAK 0x15 /* negative acknowledgement */
#define CAN 0x18 /* cancel transfer */
#define CRC 'C' /* CRC request (instead of NAK to start a
* transfer) */
/* routine status codes */
#define DT_EOF -1
#define DT_OK 1
/* number of times to retry */
#define RETRY 10
/* timeout loop counters */
#define STIME 4 /* timeout between characters of a packet */
#define LTIME 20 /* timeout waiting for an ACK/NAK */
#define XTIME 60 /* timeout waiting for initial NAK */
/* dlabort - wait for the line to clear */
static void dlabort(struct XPR_IO * IO)
{
int res;
do
res = md_get(IO, STIME);
while ((res != DT_TIME) && (res != DT_ERR));
}
/* chk_compute - compute the checksum */
static int chk_compute(unsigned char *buf, int len)
{
int chk, i;
for (chk = i = 0; i < len; ++i)
chk += buf[i];
return (chk & 0xFF);
}
/* crc_compute - compute the CRC value */
static int crc_compute(unsigned char *buf, int len)
{
int crc, shifter, highbit, i;
buf[len++] = '\0';
buf[len++] = '\0';
for (crc = i = 0; i < len; ++i)
for (shifter = 0x80; shifter; shifter >>= 1) {
highbit = crc & 0x8000;
crc <<= 1;
crc |= (buf[i] & shifter ? 1 : 0);
if (highbit)
crc ^= 0x1021;
}
return (crc & 0xFFFF);
}
static char *blkchk[] =
{"Checksum", "CRC-16"};
static char *timefmt = "%02ld:%02ld:%02ld";
static void bm_info(struct XPR_IO * IO, int error)
{
long (*xupdate) (), (*xchkmisc) (void);
struct XPR_UPDATE xpru;
long secs, micros, elapsed;
char buff1[20], buff2[20];
int blksiz, Iblknum;
blksiz=IO->xpr_data->msglength;
Iblknum=IO->xpr_data->blknum;
xchkmisc = IO->xpr_chkmisc;
if (xchkmisc)
xchkmisc();
/*
* Calculate elapsed time in tenths of seconds
*/
CurrentTime(&secs, µs);
secs -= IO->xpr_data->startsecs;
micros -= IO->xpr_data->startmics;
elapsed = (secs * 10L) + (micros / 100000L);
/*
* Always update data rate and elapsed time
*/
xpru.xpru_updatemask = XPRU_DATARATE | XPRU_ELAPSEDTIME;
sprintf(buff1, timefmt, secs / 3600L, (secs / 60L) % 60L, secs % 60L);
xpru.xpru_elapsedtime = buff1;
if (elapsed)
xpru.xpru_datarate = ((long) blksiz * Iblknum * 10L) / elapsed;
else
xpru.xpru_datarate = 0;
/*
* If we know the file size and the data rate we can compute the
* estimated time.
*/
if (IO->xpr_data->filsiz) {
if (xpru.xpru_datarate) {
xpru.xpru_updatemask |= XPRU_EXPECTTIME;
secs = IO->xpr_data->filsiz / xpru.xpru_datarate;
sprintf(buff2, timefmt, secs / 3600L, (secs / 60L) % 60L, secs % 60L);
xpru.xpru_expecttime = buff2;
}
}
/*
* On error, timeout or otherwise, update different things
*/
if (error == DT_ERR) {
IO->xpr_data->numerrs++;
xpru.xpru_updatemask |= XPRU_ERRORS;
xpru.xpru_errors = IO->xpr_data->numerrs;
} else if (error == DT_TIME) {
IO->xpr_data->numtime++;
xpru.xpru_updatemask |= XPRU_TIMEOUTS;
xpru.xpru_timeouts = IO->xpr_data->numtime;
} else {
xpru.xpru_updatemask |= XPRU_BLOCKS | XPRU_BLOCKSIZE |
XPRU_BLOCKCHECK | XPRU_BYTES;
xpru.xpru_blocks = (long) Iblknum;
xpru.xpru_blocksize = (long) blksiz;
xpru.xpru_bytes = (long) blksiz *(long) Iblknum;
xpru.xpru_blockcheck = IO->xpr_data->crcmode ? blkchk[1] : blkchk[0];
}
/*
* Do the actual update
*/
if ((xupdate = IO->xpr_update) != NULL)
calla(xupdate, &xpru);
return;
}
/* mdsnd - send a buffer of data */
static int mdsnd(struct XPR_IO * IO)
{
int plength, retries, ch;
/* setup the packet header */
IO->xpr_data->packet[0] = IO->xpr_data->msgstart;
IO->xpr_data->packet[1] = IO->xpr_data->blknum;
IO->xpr_data->packet[2] = ~IO->xpr_data->blknum;
/* compute the block check code */
if (IO->xpr_data->crcmode) { /* compute the CRC */
long dumb_index;/* workaround manx expression too complex bug */
IO->xpr_data->chksum = crc_compute(IO->xpr_data->buffer, IO->xpr_data->msglength);
IO->xpr_data->buffer[IO->xpr_data->msglength] = IO->xpr_data->chksum >> 8;
dumb_index = IO->xpr_data->msglength + 1;
IO->xpr_data->buffer[dumb_index] = IO->xpr_data->chksum;
plength = IO->xpr_data->msglength + 5;
} else { /* compute the checksum */
long dumb_index;/* workaround manx expression too complex bug */
IO->xpr_data->chksum = chk_compute(IO->xpr_data->buffer, IO->xpr_data->msglength);
dumb_index = IO->xpr_data->msglength;
IO->xpr_data->buffer[dumb_index] = IO->xpr_data->chksum;
plength = IO->xpr_data->msglength + 4;
}
/* send data and wait for an ACK */
for (retries = 0; retries < RETRY; retries++) {
/* send the data */
md_write(IO, IO->xpr_data->packet, plength);
/* return on an ACK */
if ((ch = md_get(IO, LTIME)) == ACK)
return (DT_OK);
/* abort transfer on two successive CAN's */
else if (ch == CAN) {
if ((ch = md_get(IO, STIME)) == ACK)
return (DT_OK);
else if (ch == CAN)
break;
} else if (ch == DT_ERR) {
dlabort(IO);
md_put(IO, CAN);
md_put(IO, CAN);
return (DT_ERR);
}
bm_info(IO, ch);
}
/* return failure */
return (DT_ERR);
}
/* msgput - put a message data character */
static int msgput(struct XPR_IO * IO, int ch)
{
int sts;
IO->xpr_data->buffer[IO->xpr_data->bufptr++] = ch;
if (IO->xpr_data->bufptr == IO->xpr_data->msglength) {
bm_info(IO, 0);
if ((sts = mdsnd(IO)) != DT_OK)
return (sts);
++IO->xpr_data->blknum;
IO->xpr_data->bufptr = 0;
}
return (ch);
}
/* mdrcv - receive a buffer of data */
static int mdrcv(struct XPR_IO * IO)
{
int plength, i, ch, retries;
/* receive data */
for (retries = 0; retries < RETRY + 1; retries++) {
/* check for data packet or eot */
if ((ch = md_get(IO, LTIME)) == DT_TIME) {
bm_info(IO, ch);
md_put(IO, IO->xpr_data->nak);
continue;
} else if (ch == DT_ERR) {
bm_info(IO, ch);
dlabort(IO);
md_put(IO, CAN);
md_put(IO, CAN);
return (DT_ERR);
} else if (ch == EOT) { /* end of transfer */
md_put(IO, ACK);
return (DT_EOF);
} else if (ch == SOH) /* start of a short packet */
IO->xpr_data->msglength = SMSGLEN;
else if (ch == STX) /* start of a long packet */
IO->xpr_data->msglength = LMSGLEN;
else {
bm_info(IO, DT_ERR);
dlabort(IO);
md_put(IO, IO->xpr_data->nak);
continue;
}
/* reset the NAK character */
IO->xpr_data->nak = NAK;
/* compute the packet length */
plength = IO->xpr_data->msglength + (IO->xpr_data->crcmode ? 5 : 4);
/* receive the data */
for (i = 1; i < plength; IO->xpr_data->packet[i++] = ch) {
if ((ch = md_get(IO, STIME)) == DT_TIME)
break;
else if (ch == DT_ERR)
break;
}
/* check for timeout */
if (ch == DT_TIME) {
bm_info(IO, ch);
md_put(IO, NAK);
continue;
}
if (ch == DT_ERR) {
bm_info(IO, ch);
dlabort(IO);
md_put(IO, CAN);
md_put(IO, CAN);
return (DT_ERR);
}
/* check the block number */
if (IO->xpr_data->packet[1] != (~IO->xpr_data->packet[2] & 0xFF)) {
bm_info(IO, DT_ERR);
md_put(IO, NAK);
continue;
}
/* check the block check code */
if (IO->xpr_data->crcmode) { /* CRC */
IO->xpr_data->chksum = (IO->xpr_data->buffer[IO->xpr_data->msglength] << 8) | IO->xpr_data->buffer[IO->xpr_data->msglength + 1];
if (IO->xpr_data->chksum != crc_compute(IO->xpr_data->buffer, IO->xpr_data->msglength)) {
bm_info(IO, DT_ERR);
md_put(IO, NAK);
continue;
}
} else { /* checksum */
IO->xpr_data->chksum = IO->xpr_data->buffer[IO->xpr_data->msglength];
if (IO->xpr_data->chksum != chk_compute(IO->xpr_data->buffer, IO->xpr_data->msglength)) {
bm_info(IO, DT_ERR);
md_put(IO, NAK);
continue;
}
}
/* check the block number */
if (IO->xpr_data->packet[1] == (IO->xpr_data->blknum & 0xFF)) {
md_put(IO, ACK);
return (DT_OK);
} else if (IO->xpr_data->packet[1] != ((IO->xpr_data->blknum - 1) & 0xFF)) {
bm_info(IO, DT_ERR);
dlabort(IO);
md_put(IO, CAN);
md_put(IO, CAN);
return (DT_ERR);
}
/* send a ack */
md_put(IO, ACK);
}
/* return failure */
return (DT_ERR);
}
/* msgget - get a message data character */
static int msgget(struct XPR_IO * IO)
{
int sts;
if (IO->xpr_data->bufptr == IO->xpr_data->msglength) {
if ((sts = mdrcv(IO)) != DT_OK)
return (sts);
bm_info(IO, 0);
++IO->xpr_data->blknum;
IO->xpr_data->bufptr = 0;
}
return (IO->xpr_data->buffer[IO->xpr_data->bufptr++]);
}
/* dl_snd - send data across the link */
int dl_snd(struct XPR_IO * IO, int (*getch) (struct XPR_IO * IO))
{
int ch, retries;
IO->xpr_data->abort = 0;
/* setup message length */
if (IO->xpr_data->big) {
IO->xpr_data->msgstart = STX;
IO->xpr_data->msglength = LMSGLEN;
} else {
IO->xpr_data->msgstart = SOH;
IO->xpr_data->msglength = SMSGLEN;
}
/* initialize */
IO->xpr_data->blknum = 1; /* start a block number 1 */
IO->xpr_data->bufptr = 0; /* start with an empty buffer */
/* wait for the initial NAK (or CRC) */
for (retries = 0; retries < RETRY; retries++)
if ((ch = md_get(IO, XTIME)) == NAK) { /* start of checksum
* transfer */
IO->xpr_data->crcmode = FALSE;
break;
} else if (IO->xpr_data->crc_conf && ch == CRC) { /* start of CRC transfer */
IO->xpr_data->crcmode = TRUE;
break;
} else if (ch == DT_ERR) {
bm_info(IO, DT_ERR);
dlabort(IO);
return (FALSE);
} else if (ch == DT_TIME) {
bm_info(IO, DT_TIME);
dlabort(IO);
return (FALSE);
} else
dlabort(IO);
/* check for failure */
if (retries >= RETRY)
return (FALSE);
/* send each byte */
while ((ch = (*getch) (IO)) != EOF)
if (msgput(IO, ch) == DT_ERR)
return (FALSE);
/* flush partial buffer */
if (IO->xpr_data->bufptr > 0)
while (IO->xpr_data->bufptr > 0)
if (msgput(IO, 0) == DT_ERR)
return (FALSE);
/* send EOT and wait for ACK */
for (retries = 0; retries < RETRY; retries++) {
md_put(IO, EOT);
if (md_get(IO, LTIME) == ACK)
return (TRUE);
}
/* return failure */
return (FALSE);
}
/* dl_rcv - receive data across the link */
int dl_rcv(struct XPR_IO * IO, void (*putch) (struct XPR_IO * IO, int ch))
{
int ch;
/* initialize */
IO->xpr_data->abort = 0;
IO->xpr_data->blknum = 1;
IO->xpr_data->bufptr = IO->xpr_data->msglength = SMSGLEN;
/* send the initial NAK (or CRC) */
IO->xpr_data->nak = (IO->xpr_data->crc_conf ? CRC : NAK);
IO->xpr_data->crcmode = IO->xpr_data->crc_conf;
md_put(IO, IO->xpr_data->nak);
/* receive each byte */
while ((ch = msgget(IO)) != DT_EOF && ch != DT_ERR)
(*putch) (IO, ch);
/* return with status */
return (ch == DT_EOF);
}
void bm_infoinit(struct XPR_IO * IO, int send, long size)
{
long (*xupdate) ();
struct XPR_UPDATE xpru;
char buff[20];
IO->xpr_data->numerrs = 0L;
IO->xpr_data->numtime = 0L;
if (send)
strcpy(buff, "Send ");
else
strcpy(buff, "Receive ");
if (IO->xpr_data->ascii)
strcat(buff, "Text ");
else
strcat(buff, "Binary");
xpru.xpru_updatemask = XPRU_MSG | XPRU_FILENAME |
XPRU_BLOCKSIZE | XPRU_BLOCKCHECK | XPRU_BYTES |
XPRU_BLOCKS | XPRU_TIMEOUTS | XPRU_ERRORS;
IO->xpr_data->filsiz = size;
if (size)
xpru.xpru_updatemask |= XPRU_FILESIZE;
xpru.xpru_filename = IO->xpr_filename;
xpru.xpru_filesize = size;
xpru.xpru_msg = buff;
xpru.xpru_blocks = 0L;
xpru.xpru_errors = 0L;
xpru.xpru_timeouts = 0L;
xpru.xpru_blocksize = IO->xpr_data->big ? 1024L : 128L;
xpru.xpru_blockcheck = IO->xpr_data->crc_conf ? blkchk[1] : blkchk[0];
xpru.xpru_bytes = 0L;
if ((xupdate = IO->xpr_update) != NULL)
calla(xupdate, &xpru);
CurrentTime(&IO->xpr_data->startsecs, &IO->xpr_data->startmics);
return;
}